using HKB.Common;
using System;
using System.Data;
using System.Web;
namespace HKB.Web
{
	public class Shop_Address
	{
		private Conn conn = null;
		public Shop_Address(Conn _conn)
		{
			this.conn = _conn;
		}
		public Shop_Address()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public DataTable GetAddress(int UserId)
		{
			return this.conn.GetDataTable("Select * From [HLShop_Address] Where UserID=" + UserId + " Order By AddressID");
		}
		public int InsertAddress(int UserID, string UserName, string Address, string ZipCode, string Conectphone, string MobilePhone, string Email, string Province, string City,Int16 IsDefault=0)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserName", DbType.String, UserName),
				this.conn.CreateParameter("@Address", DbType.String, Address),
				this.conn.CreateParameter("@ZipCode", DbType.String, ZipCode),
				this.conn.CreateParameter("@ConectPhone", DbType.String, Conectphone),
				this.conn.CreateParameter("@MobilePhone", DbType.String, MobilePhone),
				this.conn.CreateParameter("@Email", DbType.String, Email),
				this.conn.CreateParameter("@UserID", DbType.Int32, UserID),
				this.conn.CreateParameter("@Province", DbType.String, Province),
				this.conn.CreateParameter("@City", DbType.String, City),
                this.conn.CreateParameter("@IsDefault", DbType.Int16, IsDefault)
			};
            return this.conn.Execute("INSERT INTO [HLShop_Address] ([UserID], [UserName], [Address], [ZipCode], [ConectPhone], [MobilePhone], [Email],[Province],[City],IsDefault) VALUES (@UserID, @UserName, @Address, @ZipCode, @ConectPhone, @MobilePhone, @Email,@Province,@City,@IsDefault)", parameters);
		}
        public int InsertAddress(int UserID, string UserName, string Address, string ZipCode, string Conectphone, string MobilePhone, string Email, string Province, string City, string CMArea, Int16 IsDefault = 0)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserName", DbType.String, UserName),
				this.conn.CreateParameter("@Address", DbType.String, Address),
				this.conn.CreateParameter("@ZipCode", DbType.String, ZipCode),
				this.conn.CreateParameter("@ConectPhone", DbType.String, Conectphone),
				this.conn.CreateParameter("@MobilePhone", DbType.String, MobilePhone),
				this.conn.CreateParameter("@Email", DbType.String, Email),
				this.conn.CreateParameter("@UserID", DbType.Int32, UserID),
				this.conn.CreateParameter("@Province", DbType.String, Province),
				this.conn.CreateParameter("@City", DbType.String, City),
				this.conn.CreateParameter("@CMArea", DbType.String, CMArea),
                this.conn.CreateParameter("@IsDefault", DbType.Int16, IsDefault)
			};
            return this.conn.Execute("INSERT INTO [HLShop_Address] ([UserID], [UserName], [Address], [ZipCode], [ConectPhone], [MobilePhone], [Email],[Province],[City],[CMArea],IsDefault) VALUES (@UserID, @UserName, @Address, @ZipCode, @ConectPhone, @MobilePhone, @Email,@Province,@City,@CMArea,@IsDefault)", parameters);
		}
        public int UpdateAddress(int UserID, string UserName, string Address, string ZipCode, string Conectphone, string MobilePhone, string Email, int AddressID, string Province, string City, string CMArea, Int16 IsDefault = 0)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserName", DbType.String, UserName),
				this.conn.CreateParameter("@Address", DbType.String, Address),
				this.conn.CreateParameter("@ZipCode", DbType.String, ZipCode),
				this.conn.CreateParameter("@Conectphone", DbType.String, Conectphone),
				this.conn.CreateParameter("@MobilePhone", DbType.String, MobilePhone),
				this.conn.CreateParameter("@Email", DbType.String, Email),
				this.conn.CreateParameter("@UserID", DbType.Int32, UserID),
				this.conn.CreateParameter("@Original_AddressID", DbType.Int32, AddressID),
				this.conn.CreateParameter("@Province", DbType.String, Province),
				this.conn.CreateParameter("@City", DbType.String, City),
				this.conn.CreateParameter("@CMArea", DbType.String, CMArea),
                this.conn.CreateParameter("@IsDefault", DbType.Int16, IsDefault)
			};
            return this.conn.Execute("UPDATE [HLShop_Address] SET [UserID] = @UserID, [UserName] = @UserName, [Address] = @Address, [ZipCode] = @ZipCode, [ConectPhone] = @ConectPhone, [MobilePhone] = @MobilePhone, [Email] = @Email,[Province]=@Province,[City]=@City,[CMArea]=@CMArea,IsDefault=@IsDefault WHERE (([AddressID] = @Original_AddressID))", parameters);
		}
        public int UpdateAddressByAddressID(int AddressID, string UserName, string Address, string ZipCode, string Conectphone, string MobilePhone, string Email, string Province, string City, string CMArea, Int16 IsDefault = 0)
		{
			IDbDataParameter[] array = new IDbDataParameter[11];
			array[0] = this.conn.CreateParameter("@UserName", DbType.String, UserName);
			array[1] = this.conn.CreateParameter("@Address", DbType.String, Address);
			array[2] = this.conn.CreateParameter("@ZipCode", DbType.String, ZipCode);
			array[3] = this.conn.CreateParameter("@Conectphone", DbType.String, Conectphone);
			array[4] = this.conn.CreateParameter("@MobilePhone", DbType.String, MobilePhone);
			array[5] = this.conn.CreateParameter("@Email", DbType.String, Email);
			array[6] = this.conn.CreateParameter("@Original_AddressID", DbType.Int32, AddressID);
			array[7] = this.conn.CreateParameter("@Province", DbType.String, Province);
			array[8] = this.conn.CreateParameter("@City", DbType.String, City);
			array[9] = this.conn.CreateParameter("@CMArea", DbType.String, CMArea);
            array[10] = this.conn.CreateParameter("@IsDefault", DbType.Int16, IsDefault);
            string commandText = "UPDATE [HLShop_Address] SET [UserName] = @UserName, [Address] = @Address, [ZipCode] = @ZipCode, [ConectPhone] = @ConectPhone, [MobilePhone] = @MobilePhone, [Email] = @Email,[Province]=@Province,[City]=@City,[CMArea]=@CMArea,IsDefault=@IsDefault WHERE (([AddressID] = @Original_AddressID))";
			return this.conn.Execute(commandText, array);
		}
		public bool DeleteAddress(int AddressID, int UserID)
		{
			int num = this.conn.Execute(string.Concat(new object[]
			{
				"DELETE FROM [HLShop_Address] WHERE (([AddressID] = ",
				AddressID,
				") and UserID=",
				UserID,
				")"
			}));
			return num == 1;
		}
		public bool DeleteAddress(int AddressID)
		{
			int num = this.conn.Execute("DELETE FROM [HLShop_Address] WHERE [AddressID] = " + AddressID);
			return num == 1;
		}
        public int UpdateAddress(int UserID, string UserName, string Address, string ZipCode, string Conectphone, string MobilePhone, string Email, int AddressID, string Province, string City, Int16 IsDefault = 0)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserName", DbType.String, UserName),
				this.conn.CreateParameter("@Address", DbType.String, Address),
				this.conn.CreateParameter("@ZipCode", DbType.String, ZipCode),
				this.conn.CreateParameter("@Conectphone", DbType.String, Conectphone),
				this.conn.CreateParameter("@MobilePhone", DbType.String, MobilePhone),
				this.conn.CreateParameter("@Email", DbType.String, Email),
				this.conn.CreateParameter("@UserID", DbType.Int32, UserID),
				this.conn.CreateParameter("@Original_AddressID", DbType.Int32, AddressID),
				this.conn.CreateParameter("@Province", DbType.String, Province),
				this.conn.CreateParameter("@City", DbType.String, City),
                this.conn.CreateParameter("@IsDefault", DbType.Int16, IsDefault)
			};
            return this.conn.Execute("UPDATE [HLShop_Address] SET [UserID] = @UserID, [UserName] = @UserName, [Address] = @Address, [ZipCode] = @ZipCode, [ConectPhone] = @ConectPhone, [MobilePhone] = @MobilePhone, [Email] = @Email,[Province]=@Province,[City]=@City,IsDefault=@IsDefault WHERE (([AddressID] = @Original_AddressID))", parameters);
		}
		public bool IsMaxCount(int UserID, int num)
		{
			int num2 = this.conn.ExecuteCount("Select count(AddressID) From [HLShop_Address] Where UserID=" + UserID);
			return num2 >= num;
		}
		public DataTable GetAddressByID(int AddressID)
		{
			return this.conn.GetDataTable("Select * From [HLShop_Address] Where AddressID=" + AddressID);
		}
		public DataTable GetAddressByID(int AddressID, int UserID)
		{
			return this.conn.GetDataTable(string.Concat(new object[]
			{
				"Select * From [HLShop_Address] Where UserID=",
				UserID,
				" and AddressID=",
				AddressID
			}));
		}
		public DataTable GetAddressByUserID(int UserID)
		{
			return this.conn.GetDataTable("Select * From [HLShop_Address] Where UserID=" + UserID);
		}

        public bool UpdateDefaultAddress(int AddressID)
        {
            this.conn.Execute("UPDATE [HLShop_Address] SET IsDefault=0 WHERE [AddressID] <> " + AddressID);
            int num = this.conn.Execute("UPDATE [HLShop_Address] SET IsDefault=1 WHERE [AddressID] = " + AddressID);
            return num == 1;
        }
	}
}
